package uz.rt.dicom.data.element;

import uz.rt.dicom.ValueRepresentation;
import uz.rt.dicom.data.ByteElement;
import uz.rt.dicom.io.DicomIOException;
import uz.rt.dicom.io.DicomInputStream;
import uz.rt.dicom.tag.DicomTag;

import java.io.IOException;

public class AttributeTag extends ByteElement<DicomTag> implements Comparable<AttributeTag>
{
// --------------------------- CONSTRUCTORS ---------------------------

    public AttributeTag(DicomTag tag)
    {
        super(tag);
        setMaxLength(4);
    }

// --------------------- GETTER / SETTER METHODS ---------------------

    public DicomTag getValue()
    {
        return value;
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Comparable ---------------------


    /**
     * The function compares only the following parameters in order:
     * <ul>
     * <li>Dicom tag</li>
     * <li>Value multiplicity</li>
     * <li>the value</li>
     * </ul>
     *
     * @param o DicomTag element
     * @return A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
     *         the specified object.
     */
    public int compareTo(AttributeTag o)
    {
        if (!tag.equals(o.getTag())) return tag.compareTo(o.getTag());
        if (!vm.equals(o.getVm())) return vm.compareTo(o.getVm());
        return value.compareTo(o.getValue());
    }

// --------------------- Interface IDataElement ---------------------

    @Override
    public int getLength()
    {
        return 1;
    }

    @Override
    public byte[] getVR()
    {
        return ValueRepresentation.AT;
    }

    @Override
    public ValueRepresentation getValueRepresentation()
    {
        return ValueRepresentation.AttributeTag;
    }

    public void read(DicomInputStream dicomInputStream, int length) throws IOException
    {
        if (length > maxLength)
        {
            value = null;
            throw new DicomIOException(
                    "Byte length should be smaller or equal to the maximum byte length.");
        }
        int group = dicomInputStream.readSigned16();
        int element = dicomInputStream.readSigned16();
        value = null;
        value = new DicomTag(group, element);
    }
}
